
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/evalf.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:22 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Numerical Evaluation &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="evalf.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Functions" href="functions/index.html" />
    <link rel="prev" title="Discrete" href="discrete.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="functions/index.html" title="Functions"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="discrete.html" title="Discrete"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Numerical Evaluation</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="numerical-evaluation">
<span id="evalf-label"></span><h1>Numerical Evaluation<a class="headerlink" href="#numerical-evaluation" title="Permalink to this headline">¶</a></h1>
<section id="basics">
<h2>Basics<a class="headerlink" href="#basics" title="Permalink to this headline">¶</a></h2>
<p>Exact SymPy expressions can be converted to floating-point approximations
(decimal numbers) using either the <code class="docutils literal notranslate"><span class="pre">.evalf()</span></code> method or the <code class="docutils literal notranslate"><span class="pre">N()</span></code> function.
<code class="docutils literal notranslate"><span class="pre">N(expr,</span> <span class="pre">&lt;args&gt;)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">sympify(expr).evalf(&lt;args&gt;)</span></code>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="p">)</span>
<span class="go">4.44288293815837</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">4.44288293815837</span>
</pre></div>
</div>
<p>By default, numerical evaluation is performed to an accuracy of 15 decimal
digits. You can optionally pass a desired accuracy (which should be a positive
integer) as an argument to <code class="docutils literal notranslate"><span class="pre">evalf</span></code> or <code class="docutils literal notranslate"><span class="pre">N</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">4.4429</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="n">pi</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="go">4.4428829381583662470158809900606936986146216893757</span>
</pre></div>
</div>
<p>Complex numbers are supported:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">pi</span> <span class="o">+</span> <span class="n">I</span><span class="p">),</span> <span class="mi">20</span><span class="p">)</span>
<span class="go">0.28902548222223624241 - 0.091999668350375232456*I</span>
</pre></div>
</div>
<p>If the expression contains symbols or for some other reason cannot be evaluated
numerically, calling <code class="docutils literal notranslate"><span class="pre">.evalf()</span></code> or <code class="docutils literal notranslate"><span class="pre">N()</span></code> returns the original expression, or
in some cases a partially evaluated expression. For example, when the
expression is a polynomial in expanded form, the coefficients are evaluated:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">pi</span><span class="o">*</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">x</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">3.14159265358979*x**2 + 0.333333333333333*x</span>
</pre></div>
</div>
<p>You can also use the standard Python functions <code class="docutils literal notranslate"><span class="pre">float()</span></code>, <code class="docutils literal notranslate"><span class="pre">complex()</span></code> to
convert SymPy expressions to regular Python numbers:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">pi</span><span class="p">)</span>
<span class="go">3.1415926535...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">complex</span><span class="p">(</span><span class="n">pi</span><span class="o">+</span><span class="n">E</span><span class="o">*</span><span class="n">I</span><span class="p">)</span>
<span class="go">(3.1415926535...+2.7182818284...j)</span>
</pre></div>
</div>
<p>If these functions are used, failure to evaluate the expression to an explicit
number (for example if the expression contains symbols) will raise an exception.</p>
<p>There is essentially no upper precision limit. The following command, for
example, computes the first 100,000 digits of π/e:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">pi</span><span class="o">/</span><span class="n">E</span><span class="p">,</span> <span class="mi">100000</span><span class="p">)</span> 
<span class="gp">...</span>
</pre></div>
</div>
<p>This shows digits 999,951 through 1,000,000 of pi:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">N</span><span class="p">(</span><span class="n">pi</span><span class="p">,</span> <span class="mi">10</span><span class="o">**</span><span class="mi">6</span><span class="p">))[</span><span class="o">-</span><span class="mi">50</span><span class="p">:]</span> 
<span class="go">&#39;95678796130331164628399634646042209010610577945815&#39;</span>
</pre></div>
</div>
<p>High-precision calculations can be slow. It is recommended (but entirely
optional) to install gmpy (<a class="reference external" href="https://code.google.com/p/gmpy/">https://code.google.com/p/gmpy/</a>), which will
significantly speed up computations such as the one above.</p>
</section>
<section id="floating-point-numbers">
<h2>Floating-point numbers<a class="headerlink" href="#floating-point-numbers" title="Permalink to this headline">¶</a></h2>
<p>Floating-point numbers in SymPy are instances of the class <code class="docutils literal notranslate"><span class="pre">Float</span></code>. A <code class="docutils literal notranslate"><span class="pre">Float</span></code>
can be created with a custom precision as second argument:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="go">0.100000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">0.1000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mf">0.125</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">0.125000000000000000000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">0.100000000000000005551115123126</span>
</pre></div>
</div>
<p>As the last example shows, some Python floats are only accurate to about 15
digits as inputs, while others (those that have a denominator that is a
power of 2, like 0.125 = 1/8) are exact. To create a <code class="docutils literal notranslate"><span class="pre">Float</span></code> from a
high-precision decimal number, it is better to pass a string, <code class="docutils literal notranslate"><span class="pre">Rational</span></code>,
or <code class="docutils literal notranslate"><span class="pre">evalf</span></code> a <code class="docutils literal notranslate"><span class="pre">Rational</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;0.1&#39;</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">0.100000000000000000000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">0.100000000000000000000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="go">0.100000000000000000000000000000</span>
</pre></div>
</div>
<p>The precision of a number determines 1) the precision to use when performing
arithmetic with the number, and 2) the number of digits to display when printing
the number. When two numbers with different precision are used together in an
arithmetic operation, the higher of the precisions is used for the result. The
product of 0.1 +/- 0.001 and 3.1415 +/- 0.0001 has an uncertainty of about 0.003
and yet 5 digits of precision are shown.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span><span class="o">*</span><span class="n">Float</span><span class="p">(</span><span class="mf">3.1415</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">0.31417</span>
</pre></div>
</div>
<p>So the displayed precision should not be used as a model of error propagation or
significance arithmetic; rather, this scheme is employed to ensure stability of
numerical algorithms.</p>
<p><code class="docutils literal notranslate"><span class="pre">N</span></code> and <code class="docutils literal notranslate"><span class="pre">evalf</span></code> can be used to change the precision of existing
floating-point numbers:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="mf">3.5</span><span class="p">)</span>
<span class="go">3.50000000000000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="mf">3.5</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">3.5000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="mf">3.5</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">3.50000000000000000000000000000</span>
</pre></div>
</div>
</section>
<section id="accuracy-and-error-handling">
<h2>Accuracy and error handling<a class="headerlink" href="#accuracy-and-error-handling" title="Permalink to this headline">¶</a></h2>
<p>When the input to <code class="docutils literal notranslate"><span class="pre">N</span></code> or <code class="docutils literal notranslate"><span class="pre">evalf</span></code> is a complicated expression, numerical
error propagation becomes a concern. As an example, consider the 100’th
Fibonacci number and the excellent (but not exact) approximation <span class="math notranslate nohighlight">\(\varphi^{100} / \sqrt{5}\)</span>
where <span class="math notranslate nohighlight">\(\varphi\)</span> is the golden ratio. With ordinary floating-point arithmetic,
subtracting these numbers from each other erroneously results in a complete
cancellation:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">GoldenRatio</span><span class="o">**</span><span class="mi">1000</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">fibonacci</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">4.34665576869e+208</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">4.34665576869e+208</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">-</span> <span class="nb">float</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">0.0</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">N</span></code> and <code class="docutils literal notranslate"><span class="pre">evalf</span></code> keep track of errors and automatically increase the
precision used internally in order to obtain a correct result:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">fibonacci</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span> <span class="o">-</span> <span class="n">GoldenRatio</span><span class="o">**</span><span class="mi">100</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="go">-5.64613129282185e-22</span>
</pre></div>
</div>
<p>Unfortunately, numerical evaluation cannot tell an expression that is exactly
zero apart from one that is merely very small. The working precision is
therefore capped, by default to around 100 digits. If we try with the 1000’th
Fibonacci number, the following happens:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">fibonacci</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">GoldenRatio</span><span class="p">)</span><span class="o">**</span><span class="mi">1000</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="go">0.e+85</span>
</pre></div>
</div>
<p>The lack of digits in the returned number indicates that <code class="docutils literal notranslate"><span class="pre">N</span></code> failed to achieve
full accuracy. The result indicates that the magnitude of the expression is something
less than 10^84, but that is not a particularly good answer. To force a higher working
precision, the <code class="docutils literal notranslate"><span class="pre">maxn</span></code> keyword argument can be used:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">fibonacci</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">GoldenRatio</span><span class="p">)</span><span class="o">**</span><span class="mi">1000</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">maxn</span><span class="o">=</span><span class="mi">500</span><span class="p">)</span>
<span class="go">-4.60123853010113e-210</span>
</pre></div>
</div>
<p>Normally, <code class="docutils literal notranslate"><span class="pre">maxn</span></code> can be set very high (thousands of digits), but be aware that
this may cause significant slowdown in extreme cases. Alternatively, the
<code class="docutils literal notranslate"><span class="pre">strict=True</span></code> option can be set to force an exception instead of silently
returning a value with less than the requested accuracy:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">fibonacci</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">GoldenRatio</span><span class="p">)</span><span class="o">**</span><span class="mi">1000</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">strict</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">PrecisionExhausted</span>: <span class="n">Failed to distinguish the expression:</span>

<span class="go">-sqrt(5)*GoldenRatio**1000/5 + 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875</span>

<span class="go">from zero. Try simplifying the input, using chop=True, or providing a higher maxn for evalf</span>
</pre></div>
</div>
<p>If we add a term so that the Fibonacci approximation becomes exact (the full
form of Binet’s formula), we get an expression that is exactly zero, but <code class="docutils literal notranslate"><span class="pre">N</span></code>
does not know this:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">fibonacci</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">GoldenRatio</span><span class="o">**</span><span class="mi">100</span> <span class="o">-</span> <span class="p">(</span><span class="n">GoldenRatio</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">**</span><span class="mi">100</span><span class="p">)</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="go">0.e-104</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">maxn</span><span class="o">=</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">0.e-1336</span>
</pre></div>
</div>
<p>In situations where such cancellations are known to occur, the <code class="docutils literal notranslate"><span class="pre">chop</span></code> options
is useful. This basically replaces very small numbers in the real or
imaginary portions of a number with exact zeros:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">chop</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="mi">3</span> <span class="o">+</span> <span class="n">I</span><span class="o">*</span><span class="n">f</span><span class="p">,</span> <span class="n">chop</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">3.00000000000000</span>
</pre></div>
</div>
<p>In situations where you wish to remove meaningless digits, re-evaluation or
the use of the <code class="docutils literal notranslate"><span class="pre">round</span></code> method are useful:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;.1&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">*</span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;.12345&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="go">0.012297</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ans</span> <span class="o">=</span> <span class="n">_</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">0.01</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ans</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0.01</span>
</pre></div>
</div>
<p>If you are dealing with a numeric expression that contains no floats, it
can be evaluated to arbitrary precision. To round the result relative to
a given decimal, the round method is useful:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="mi">10</span><span class="o">*</span><span class="n">pi</span> <span class="o">+</span> <span class="n">cos</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="go">31.9562288417661</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">31.956</span>
</pre></div>
</div>
</section>
<section id="sums-and-integrals">
<h2>Sums and integrals<a class="headerlink" href="#sums-and-integrals" title="Permalink to this headline">¶</a></h2>
<p>Sums (in particular, infinite series) and integrals can be used like regular
closed-form expressions, and support arbitrary-precision evaluation:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="p">(</span><span class="s1">&#39;n x&#39;</span><span class="p">)</span>
<span class="go">(n, x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">n</span><span class="o">**</span><span class="n">n</span><span class="p">,</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">1.29128599706266</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">1.29128599706266</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">n</span><span class="o">**</span><span class="n">n</span><span class="p">,</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="go">1.2912859970626635404072825905956005414986193682745</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="go">1.2912859970626635404072825905956005414986193682745</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">Integral</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">-</span><span class="n">oo</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="go">3.14159265358979323846264338328</span>
</pre></div>
</div>
<p>By default, the tanh-sinh quadrature algorithm is used to evaluate integrals.
This algorithm is very efficient and robust for smooth integrands (and even
integrals with endpoint singularities), but may struggle with integrals that
are highly oscillatory or have mid-interval discontinuities. In many cases,
<code class="docutils literal notranslate"><span class="pre">evalf</span></code>/<code class="docutils literal notranslate"><span class="pre">N</span></code> will correctly estimate the error. With the following integral,
the result is accurate but only good to four digits:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">2.346</span>
</pre></div>
</div>
<p>It is better to split this integral into two pieces:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">Integral</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">pi</span><span class="p">))</span> <span class="o">+</span> <span class="n">Integral</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">pi</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">2.34635637913639</span>
</pre></div>
</div>
<p>A similar example is the following oscillatory integral:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="n">maxn</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
<span class="go">0.5</span>
</pre></div>
</div>
<p>It can be dealt with much more efficiently by telling <code class="docutils literal notranslate"><span class="pre">evalf</span></code> or <code class="docutils literal notranslate"><span class="pre">N</span></code> to
use an oscillatory quadrature algorithm:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="n">quad</span><span class="o">=</span><span class="s1">&#39;osc&#39;</span><span class="p">)</span>
<span class="go">0.504067061906928</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Integral</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="n">quad</span><span class="o">=</span><span class="s1">&#39;osc&#39;</span><span class="p">)</span>
<span class="go">0.50406706190692837199</span>
</pre></div>
</div>
<p>Oscillatory quadrature requires an integrand containing a factor cos(ax+b) or
sin(ax+b). Note that many other oscillatory integrals can be transformed to
this form with a change of variables:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">init_printing</span><span class="p">(</span><span class="n">use_unicode</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">wrap_line</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">intgrl</span> <span class="o">=</span> <span class="n">Integral</span><span class="p">(</span><span class="n">sin</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">x</span><span class="p">),</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">1</span><span class="o">/</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">intgrl</span>
<span class="go"> oo</span>
<span class="go">  /</span>
<span class="go"> |</span>
<span class="go"> |  sin(x)</span>
<span class="go"> |  ------ dx</span>
<span class="go"> |     2</span>
<span class="go"> |    x</span>
<span class="go"> |</span>
<span class="go">/</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">intgrl</span><span class="p">,</span> <span class="n">quad</span><span class="o">=</span><span class="s1">&#39;osc&#39;</span><span class="p">)</span>
<span class="go">0.504067061906928</span>
</pre></div>
</div>
<p>Infinite series use direct summation if the series converges quickly enough.
Otherwise, extrapolation methods (generally the Euler-Maclaurin formula but
also Richardson extrapolation) are used to speed up convergence. This allows
high-precision evaluation of slowly convergent series:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="p">(</span><span class="s1">&#39;k&#39;</span><span class="p">)</span>
<span class="go">k</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">k</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">1.64493406684823</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">zeta</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">1.64493406684823</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">k</span><span class="o">-</span><span class="n">log</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1</span><span class="o">/</span><span class="n">k</span><span class="p">),</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">0.577215664901533</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">k</span><span class="o">-</span><span class="n">log</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1</span><span class="o">/</span><span class="n">k</span><span class="p">),</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="go">0.57721566490153286060651209008240243104215933593992</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">EulerGamma</span><span class="o">.</span><span class="n">evalf</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span>
<span class="go">0.57721566490153286060651209008240243104215933593992</span>
</pre></div>
</div>
<p>The Euler-Maclaurin formula is also used for finite series, allowing them to
be approximated quickly without evaluating all terms:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">k</span><span class="p">,</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="mi">10000000</span><span class="p">,</span> <span class="mi">20000000</span><span class="p">))</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">0.693147255559946</span>
</pre></div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">evalf</span></code> makes some assumptions that are not always optimal. For
fine-tuned control over numerical summation, it might be worthwhile to manually
use the method <code class="docutils literal notranslate"><span class="pre">Sum.euler_maclaurin</span></code>.</p>
<p>Special optimizations are used for rational hypergeometric series (where the
term is a product of polynomials, powers, factorials, binomial coefficients and
the like). <code class="docutils literal notranslate"><span class="pre">N</span></code>/<code class="docutils literal notranslate"><span class="pre">evalf</span></code> sum series of this type very rapidly to high
precision. For example, this Ramanujan formula for pi can be summed to 10,000
digits in a fraction of a second with a simple command:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">factorial</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">R</span> <span class="o">=</span> <span class="mi">9801</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span><span class="o">/</span><span class="n">Sum</span><span class="p">(</span><span class="n">f</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">n</span><span class="p">)</span><span class="o">*</span><span class="p">(</span><span class="mi">1103</span><span class="o">+</span><span class="mi">26390</span><span class="o">*</span><span class="n">n</span><span class="p">)</span><span class="o">/</span><span class="n">f</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="o">**</span><span class="mi">4</span><span class="o">/</span><span class="mi">396</span><span class="o">**</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">n</span><span class="p">),</span>
<span class="gp">... </span>                        <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">oo</span><span class="p">))</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">N</span><span class="p">(</span><span class="n">R</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span> 
<span class="go">3.141592653589793238462643383279502884197169399375105820974944592307816406286208</span>
<span class="go">99862803482534211706798214808651328230664709384460955058223172535940812848111745</span>
<span class="go">02841027019385211055596446229489549303819644288109756659334461284756482337867831</span>
<span class="gp">...</span>
</pre></div>
</div>
</section>
<section id="numerical-simplification">
<h2>Numerical simplification<a class="headerlink" href="#numerical-simplification" title="Permalink to this headline">¶</a></h2>
<p>The function <code class="docutils literal notranslate"><span class="pre">nsimplify</span></code> attempts to find a formula that is numerically equal
to the given input. This feature can be used to guess an exact formula for an
approximate floating-point input, or to guess a simpler formula for a
complicated symbolic input. The algorithm used by <code class="docutils literal notranslate"><span class="pre">nsimplify</span></code> is capable of
identifying simple fractions, simple algebraic expressions, linear combinations
of given constants, and certain elementary functional transformations of any of
the preceding.</p>
<p>Optionally, <code class="docutils literal notranslate"><span class="pre">nsimplify</span></code> can be passed a list of constants to include (e.g. pi)
and a minimum numerical tolerance. Here are some elementary examples:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="go">1/10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">6.28</span><span class="p">,</span> <span class="p">[</span><span class="n">pi</span><span class="p">],</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="go">2*pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">pi</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.01</span><span class="p">)</span>
<span class="go">22/7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">pi</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.001</span><span class="p">)</span>
<span class="go">355</span>
<span class="go">---</span>
<span class="go">113</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">0.33333</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">)</span>
<span class="go">1/3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">2.0</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mf">3.</span><span class="p">),</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.001</span><span class="p">)</span>
<span class="go">635</span>
<span class="go">---</span>
<span class="go">504</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mf">2.0</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mf">3.</span><span class="p">),</span> <span class="n">tolerance</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">full</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">3 ___</span>
<span class="go">\/ 2</span>
</pre></div>
</div>
<p>Here are several more advanced examples:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">Float</span><span class="p">(</span><span class="s1">&#39;0.130198866629986772369127970337&#39;</span><span class="p">,</span><span class="mi">30</span><span class="p">),</span> <span class="p">[</span><span class="n">pi</span><span class="p">,</span> <span class="n">E</span><span class="p">])</span>
<span class="go">    1</span>
<span class="go">----------</span>
<span class="go">5*pi</span>
<span class="go">---- + 2*e</span>
<span class="go"> 7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="n">atan</span><span class="p">(</span><span class="s1">&#39;1/3&#39;</span><span class="p">)))</span>
<span class="go">    ____</span>
<span class="go">3*\/ 10</span>
<span class="go">--------</span>
<span class="go">   10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mi">4</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">5</span><span class="p">)),</span> <span class="p">[</span><span class="n">GoldenRatio</span><span class="p">])</span>
<span class="go">-2 + 2*GoldenRatio</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="n">exp</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">atan</span><span class="p">(</span><span class="s1">&#39;1/4&#39;</span><span class="p">)</span><span class="o">*</span><span class="n">I</span><span class="p">))</span>
<span class="go">49   8*I</span>
<span class="go">-- + ---</span>
<span class="go">17    17</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">((</span><span class="mi">1</span><span class="o">/</span><span class="p">(</span><span class="n">exp</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">pi</span><span class="o">*</span><span class="n">I</span><span class="o">/</span><span class="mi">5</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)))</span>
<span class="go">           ___________</span>
<span class="go">          /   ___</span>
<span class="go">1        /  \/ 5    1</span>
<span class="go">- - I*  /   ----- + -</span>
<span class="go">2     \/      10    4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">I</span><span class="o">**</span><span class="n">I</span><span class="p">,</span> <span class="p">[</span><span class="n">pi</span><span class="p">])</span>
<span class="go"> -pi</span>
<span class="go"> ----</span>
<span class="go">  2</span>
<span class="go">e</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">Sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">n</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">oo</span><span class="p">)),</span> <span class="p">[</span><span class="n">pi</span><span class="p">])</span>
<span class="go">  2</span>
<span class="go">pi</span>
<span class="go">---</span>
<span class="go"> 6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nsimplify</span><span class="p">(</span><span class="n">gamma</span><span class="p">(</span><span class="s1">&#39;1/4&#39;</span><span class="p">)</span><span class="o">*</span><span class="n">gamma</span><span class="p">(</span><span class="s1">&#39;3/4&#39;</span><span class="p">),</span> <span class="p">[</span><span class="n">pi</span><span class="p">])</span>
<span class="go">  ___</span>
<span class="go">\/ 2 *pi</span>
</pre></div>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Numerical Evaluation</a><ul>
<li><a class="reference internal" href="#basics">Basics</a></li>
<li><a class="reference internal" href="#floating-point-numbers">Floating-point numbers</a></li>
<li><a class="reference internal" href="#accuracy-and-error-handling">Accuracy and error handling</a></li>
<li><a class="reference internal" href="#sums-and-integrals">Sums and integrals</a></li>
<li><a class="reference internal" href="#numerical-simplification">Numerical simplification</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="discrete.html"
                        title="previous chapter">Discrete</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="functions/index.html"
                        title="next chapter">Functions</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/modules/evalf.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="functions/index.html" title="Functions"
             >next</a> |</li>
        <li class="right" >
          <a href="discrete.html" title="Discrete"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Numerical Evaluation</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/evalf.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:23 GMT -->
</html>